<template>
	<scroll-view class="pageContent" scroll-y>
		<view v-if="list.length>0">
			<uni-collapse accordion="true">
				<view class="orderItem" v-for="(item , index ) in list" :key="index">
					<view class="itmeUpper" @click="toDetail(item)">
						<image :src="item.img" mode="aspectFill" class="goodPic"></image>
						<view class="ordercontext">
							<view class="timerandcode">
								<view class="">
									名称： <text>{{item.name}}</text>
								</view>
								<view style="color: #929295;">
									人数： <text>{{item.curUserCount}}/11</text>
								</view>
							</view>
							<view class="lineTwo">
								<view class="timerandcode">
									<view class="orderNo">
										价格：<text>{{item.price}}</text>
									</view>
									<view class="orderCode">
										奖励积分：<text>{{item.rebate_gg_credit}}</text>
									</view>
								</view>
							</view>
							<view class="" v-if="item.startTime">
								开团时间：<text>{{item.startTime}}</text>
							</view>
							<view class="" v-else>
								发起时间：<text>{{item.createTime}}</text>
							</view>
						</view>

					</view>
					<uni-collapse-item title="奖励礼品">
						<view class="descption">
							{{item.rewardDesc}}
						</view>
					</uni-collapse-item>
				</view>
			</uni-collapse>

		</view>
		<empty v-else emptyText="当前暂无可参与拼团"></empty>
		<payDia ref="pay" @commitJoin='getRequest' :sourceId="selectPin?selectPin.id: 0" :selectPrice="selectPin?selectPin.price: 0"></payDia>
	</scroll-view>
</template>

<script>
	import empty from '@/components/empty.vue'
	import payDia from "@/components/payDiia.vue"
	import uniCollapse from '@/components/uni-collapse/uni-collapse.vue'
	import uniCollapseItem from '@/components/uni-collapse-item/uni-collapse-item.vue'
	export default {
		components: {
			empty,
			payDia,
			uniCollapse,
			uniCollapseItem
		},
		data() {
			return {
				list: [],
				selectPin: null,
				invitedId: null
			};
		},
		onShow() {
			this.loadData()
		},
		onLoad(option) {
			if (option.invitedId) {
				this.invitedId = option.invitedId
			}
		},
		onHide() {
			this.invitedId = null
		},
		onUnload() {
			this.invitedId = null
		},
		methods: {
			change() {},
			getRequest(result) {
				if (result) {
					uni.setStorageSync('currentPin', this.selectPin.id)
					uni.navigateTo({
						url: '/pages/lottery/lottery'
					})
				} else {
					console.log('cancle')
				}
			},
			// 加载拼团列表
			loadData() {
				let that = this
				that.$Request.request('/api/pintuan/all', 'GET', {}).then(res => {
					if (res.status == 1) {
						if (res.data.list.length > 0) {
							that.list = res.data.list
							if (that.invitedId) {
								let select = that.list.find(function(item) {
									if (item.id == that.invitedId) {
										return item
									}
								})
								uni.showModal({
									title: '受邀提现',
									content: `你的好友邀请你参加${select.name}拼团，是否参与`,
									cancelText: '在想想',
									confirmText: '参加',
									success: (res) => {
										if (res.confirm) {
											that.toDetail(select)
										}
									}
								})

							}
						}
					}
				}).catch(err => {
					console.log(err)
				})
			},
			// 参与拼团
			toDetail(item) {
				if (item.isMy) {
					uni.navigateTo({
						url: '/pages/lottery/lottery?pinId=' + item.id
					})
				} else {

					item.price = parseFloat(item.price)
					this.selectPin = item
					this.$refs.pay.beginPay()
				}

			},

		}
	}
</script>

<style lang="scss">
	@import './tourList.scss';
</style>
